//
//  CompanyDetailsViewController.m
//  InviteJob
//
//  Created by chirui on 2017/10/24.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "CompanyDetailsViewController.h"
#import "WorkListTableViewCell.h"
#import "WorkDetailsViewController.h"

@interface CompanyDetailsViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSDictionary * dataDic;
    NSMutableArray * _dataArray;
    int _page;
    int _totailPage;
}

@end

@implementation CompanyDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    TitleView(@"公司详情");
    
    _page =1;
    _dataArray = @[].mutableCopy;
    self.view.backgroundColor =[UIColor whiteColor];
    [self creatTableView];
    
    [self getMessageListData];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = strColor(@"f6f6f6");
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[WorkListTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[WorkListTableViewCell reuseIdentifier]];
    MJRefreshNormalHeader * header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        _page = 1;
        
        [self getMessageListData];
    }];
    _tableView.mj_header = header;
    
    
    MJRefreshAutoNormalFooter *  footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getMessageListData];
    }];
    
    footer.automaticallyRefresh = YES;
    _tableView.mj_footer =footer;
    _tableView.mj_footer.hidden = NO;

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (dataDic) {
        NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",dataDic[@"company_brief"]]];
        
        
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:7];
        [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [[NSString stringWithFormat:@"%@",dataDic[@"company_brief"]] length])];
        UILabel * workDetailsLabel = [self creatLabelFrame:CGRectMake(10, 145,WIDTH -20, 20) andTextColor:RGB(0x333333) andFount:15 andTextStr:@"" andAlignment:NSTextAlignmentLeft onView:nil];
        workDetailsLabel.attributedText = attributedString1;
        workDetailsLabel.numberOfLines = 0;
        [workDetailsLabel sizeToFit];

        return CGRectGetMaxY(workDetailsLabel.frame)+185;
    }else{
    
        return 200;
    }
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary * dic = _dataArray[indexPath.row];
    WorkListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[WorkListTableViewCell reuseIdentifier]];
    cell.workName.text =  dic[@"cate_name"];
     cell.details.text =  [NSString stringWithFormat:@"%@%@ %@ %@",dic[@"city_name"],dic[@"county_name"],dic[@"work_experience"],dic[@"education"]];
     cell.priceLabel.text =  dic[@"salary"];
     cell.timeLabel.text =  dic[@"update_time"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     NSDictionary * dic = _dataArray[indexPath.row];
    WorkDetailsViewController * work =[[WorkDetailsViewController alloc]init];
    work.workIdStr=dic[@"zp_id"];
    [self.navigationController pushViewController:work animated:YES];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView * heardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 500)];
    heardView.backgroundColor = [UIColor whiteColor];

    
    if (dataDic) {
        
    UIImageView * companyImage =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    [companyImage sd_setImageWithURL:ImageUrl(dataDic[@"company_logo"]) placeholderImage:ImageName(@"")];    companyImage.layer.cornerRadius = 25.f;
    companyImage.layer.masksToBounds = YES;
    [heardView addSubview:companyImage];
    
    UILabel * comLabel = [self creatLabelFrame:CGRectMake(65, 15,30, 20) andTextColor:RGB(0x333333) andFount:15 andTextStr:dataDic[@"company_name"] andAlignment:NSTextAlignmentLeft onView:heardView];
    comLabel.numberOfLines = 1;
    [comLabel sizeToFit];
    
    [self creatTagLabelFrame:CGRectMake(CGRectGetMaxX(comLabel.frame)+5,15, 20, 14) andText:[NSString stringWithFormat:@" %@ ",dataDic[@"com_type"]] onView:heardView];
    
    [self creatNoNameLabelFrame:CGRectMake(65, 40,200, 20) andTextColor:RGB(0x999999) andFount:14 andTextStr:dataDic[@"staff_number"] andAlignment:NSTextAlignmentLeft onView:heardView];
    
    [self creatLineViewFrame:CGRectMake(0, 70, WIDTH, 10)  onView:heardView]; // 分割线
    
    
    [self creatNoNameLabelFrame:CGRectMake(10, 95,200, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:@"公司简介" andAlignment:NSTextAlignmentLeft onView:heardView];
    
    [self creatLineViewFrame:CGRectMake(0, 130, WIDTH, 0.5)  onView:heardView]; // 分割线
    
    
    
    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",dataDic[@"company_brief"]]];
    
    
    NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle1 setLineSpacing:7];
    [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [[NSString stringWithFormat:@"%@",dataDic[@"company_brief"]] length])];
    
    UILabel * workDetailsLabel = [self creatLabelFrame:CGRectMake(10, 145,WIDTH -20, 20) andTextColor:RGB(0x333333) andFount:15 andTextStr:@"" andAlignment:NSTextAlignmentLeft onView:heardView];
    workDetailsLabel.attributedText = attributedString1;
    
    workDetailsLabel.numberOfLines = 0;
    [workDetailsLabel sizeToFit];
    
    
    [self creatLineViewFrame:CGRectMake(0, CGRectGetMaxY(workDetailsLabel.frame)+15, WIDTH, 10)  onView:heardView]; // 分割线
    
    
    
    UIView * endView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(workDetailsLabel.frame)+25, WIDTH, 100)];
    [heardView addSubview:endView];
    
    
    [self creatNoNameLabelFrame:CGRectMake(10, 15,200, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:@"工作地址" andAlignment:NSTextAlignmentLeft onView:endView];
    
    [self creatLineViewFrame:CGRectMake(0, 50, WIDTH, 0.5)  onView:endView];
    
    UIImageView * addImage =[[UIImageView alloc]initWithFrame:CGRectMake(10, 67, 16, 16)];
    addImage.image = ImageName(@"icon_details_address");
    [endView addSubview:addImage];
    
     [self creatNoNameLabelFrame:CGRectMake(30, 65,WIDTH - 40, 20) andTextColor:[UIColor grayColor] andFount:15 andTextStr:[NSString stringWithFormat:@"%@%@%@",dataDic[@"province_name"],dataDic[@"city_name"],dataDic[@"county_name"]] andAlignment:NSTextAlignmentLeft onView:endView];
    
     [self creatLineViewFrame:CGRectMake(0, CGRectGetMaxY(endView.frame), WIDTH,10)  onView:heardView];
    
    [self creatNoNameLabelFrame:CGRectMake(10, CGRectGetMaxY(endView.frame)+25,WIDTH - 20, 20) andTextColor:[UIColor blackColor] andFount:16 andTextStr:@"招聘职位" andAlignment:NSTextAlignmentLeft onView:heardView];
    }
    
    return heardView;
}

-(void)getMessageListData{
    
    if (_totailPage != 0) {
        if(_page == _totailPage + 1){
            [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
            
            return ;
        }
    }
    NSDictionary * dic = @{
                           @"company_id":self.company_idStr,
                           @"page":[NSString stringWithFormat:@"%d",_page],
                           @"perpage":@"10"
                           };
    
    SHuiMBhubNoTimeStr(@"正在加载...");
    [PostNetWorking postNetWorkingStr:RequestUrlStr(main_getCompanyDetails_api) andparameters:dic Success:^(id responseObject) {
        SHuiMBHidleYes;
        if (codeSucess) {
            _totailPage = [responseObject[@"datas"][@"total_page"]  intValue];
            dataDic = responseObject[@"datas"][@"company_info"];
            if (_page==1) {
                
                if (_dataArray) {
                    
                    [_dataArray removeAllObjects];
                }
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }else {
                
                if ([responseObject[@"datas"][@"lists"]  isKindOfClass:[NSArray class]]) {
                    
                    for (NSDictionary * dic in responseObject[@"datas"][@"lists"]) {
                        
                        [_dataArray addObject:dic];
                    }
                }
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
                if (_dataArray.count>0) {
                    
                    if (_dataArray.count<10) {
                        
                        _tableView.mj_footer.hidden = YES;
                    }else {
                        _tableView.mj_footer.hidden = NO;
                    }
                }else {
                    _tableView.mj_footer.hidden = YES;
                    
                }
                [_tableView.mj_header endRefreshing];
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
                
            });
            
        }else {
            
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
    } failed:^(NSError *error) {
        SHuiMBHidleYes;
        SHuiMBhudTextTimeStr(@"网络错误请重试！");
    }];
    
    
}


-(UILabel *)creatLabelFrame:(CGRect)rec andTextColor:(UIColor *)col andFount:(CGFloat)fount andTextStr:(NSString *)str andAlignment:(NSTextAlignment)alignment onView:(UIView *)view{
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font =[UIFont systemFontOfSize:fount];
    label.textColor =col;
    label.text = str;
    label.textAlignment = alignment;
    [view addSubview:label];
    return label;
}

-(void)creatTagLabelFrame:(CGRect)rec andText:(NSString *)str onView:(UIView *)view{
    
    UILabel * tag_one =[[UILabel alloc]initWithFrame:rec];
    tag_one.backgroundColor =[UIColor orangeColor];
    tag_one.textColor =[UIColor whiteColor];
    tag_one.textAlignment = NSTextAlignmentCenter;
    tag_one.font = [UIFont systemFontOfSize:14];
    tag_one.layer.cornerRadius = 4.f;
    tag_one.layer.masksToBounds = YES;
    tag_one.text = str;
    tag_one.numberOfLines = 1;
    [tag_one sizeToFit];
    [view addSubview:tag_one];
}

-(void)creatNoNameLabelFrame:(CGRect)rec andTextColor:(UIColor *)col andFount:(CGFloat)fount andTextStr:(NSString *)str andAlignment:(NSTextAlignment)alignment onView:(UIView *)view{
    
    UILabel * label =[[UILabel alloc]initWithFrame:rec];
    label.font =[UIFont systemFontOfSize:fount];
    label.textColor =col;
    label.text = str;
    label.textAlignment = alignment;
    [view addSubview:label];
}

-(void)creatLineViewFrame:(CGRect)rec onView:(UIView *)view{
    
    UIView * lineView = [[UIView alloc]initWithFrame:rec];
    lineView.backgroundColor = strColor(@"f6f6f6");
    [view addSubview:lineView];
}

@end
